package com.wang.bootbuilder.controller;

import com.wang.bootbuilder.annotation.HasPermission;
import com.wang.bootbuilder.entity.param.AddOrUpdateProductParam;
import com.wang.bootbuilder.entity.param.SelectProductByPageParam;
import com.wang.bootbuilder.entity.param.ShelfProductParam;
import com.wang.bootbuilder.service.ProductService;
import com.wang.bootbuilder.tools.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

@RestController
@RequestMapping("/manage/product")
@Api(value = "/manage/category", description = "处理商品相关请求的controller")
public class ProductController {

    @Autowired
    ProductService productService;

    /*
    获取商品分页列表
     */
    @PostMapping("/list")
    @HasPermission("3")
    public JsonResult getProductListByPage(@RequestBody @Validated SelectProductByPageParam param) {
        JsonResult result = productService.getProductListByPage(param);
        return result;
    }

    /*
    根据ID/Name搜索产品分页列表
     */
    @PostMapping("/search")
    public JsonResult getConditionProductListByPage(@RequestBody @Validated SelectProductByPageParam param) {
        JsonResult result = productService.getConditionProductListByPage(param);
        return result;
    }

    /*
    添加商品
     */
    @PostMapping("/add")
    public JsonResult addProduct(@RequestBody @Validated AddOrUpdateProductParam param) {
        JsonResult result = productService.addProduct(param);
        return result;
    }

    /*
    删除商品
     */
    @GetMapping("/delete")
    public JsonResult deleteProduct(@RequestParam String productId) {
        JsonResult result = productService.deleteProduct(productId);
        return result;
    }

    /*
    对商品进行上架/下架处理
     */
    @PostMapping("/updateStatus")
    public JsonResult onOffShelfProduct(@RequestBody @Validated ShelfProductParam param) {
        JsonResult result = productService.onOffShelfProduct(param.getProductId(), param.getStatus());
        return result;
    }

    /*
    更新商品
     */
    @PostMapping("/update")
    public JsonResult updateProduct(@Validated @RequestBody AddOrUpdateProductParam param) {
        JsonResult result = productService.updateProduct(param);
        return result;
    }
}
